package web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.TankSicherheit;

/**
 * Servlet implementation class UserRegisterServlet
 */
public class UserRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RequestDispatcher jsp;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserRegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
	public void init(ServletConfig config) throws ServletException {
		ServletContext context = config.getServletContext();
		jsp = context.getRequestDispatcher("/WEB-INF/jsp/register.jsp");
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		jsp.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		TankSicherheit.LOGGER.debug("UserRegisterServlet");
		try
		{
			String wotusername = request.getParameter("username");
			System.out.println(wotusername);
			String password = request.getParameter("password");
			System.out.println(password);
			int wotaccountid = Integer.parseInt(request.getParameter("wotaccountid"));
			System.out.println(wotaccountid);
			
			if(wotusername!=null&&password!=null&&wotaccountid>0)
			{
				TankSicherheit.staticCreateUserFromForm(wotusername, password, wotaccountid);
				response.sendRedirect("login");
			}
			else
			{
				TankSicherheit.LOGGER.info("user not created");
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			TankSicherheit.LOGGER.warn("Exception on UserRegisterServlet's DoPost:"+e);
		}

		
	}

}
